;
(function() {
	jadeSystem.validateInstall();

	/**
	 * 场景
	 * @param {Object} name 场景名
	 * @param {Object} layerGroup 层组
	 * @param {Object} sprites 精灵
	 * @param {Object} eventSupport 支持事件
	 */
	JadeEngine.Scene = function(name, layerGroup, sprites, eventSupport) {
		this.id = GUID.next();
		this.name = name;
		this.layerGroup = layerGroup;
		this.eventSupport = eventSupport;
		this.startLocation = {
			x: 0,
			y: 0
		};
		this.width = 2;
		this.height = 2;
		this.onEvent = function() {

		}
		if (typeof sprites === "undefined") {
			this.sprites = [];
		}

		if (typeof sprites === "object") {
			this.sprites = [];
			this.sprites.push(sprites)
		}
		if (jadeSystem.isUndefine(this.eventSupport)) {
			this.eventSupport = true;
			JadeEngine.event.pool[this.id] = this;
		}
	}

	/**
	 * 层组
	 * @param {Object} layers 层
	 */
	JadeEngine.LayerGroup = function(layers) {
		this.id = GUID.next();
		this.layers = layers;

		if (jadeSystem.isUndefine(this.layers)) {
			this.layers = [];
		}

		this.addLayer = function() {
			var layer = null;

			if (arguments.length = 1) {
				layer = arguments[0];
				this.layers.push(layer);
			}

			if (arguments.length = 2) {
				index = arguments[0]
				this.layers[index] = arguments[1];
			}
		}

		this.getLocations = function() {
			var x = -1;
			var y = -1;
			if (arguments.length == 1) {
				x = arguments[0].x;
				y = arguments[0].y;
			}

			if (arguments.length == 2) {
				x = arguments[0];
				y = arguments[1];
			}
			var results = jadeSystem.notifyArrayAndResult(this.layers, "getLocation", {
				"x": x,
				"y": y
			});
			return results;
		}

	}

	/**
	 * 层
	 * @param {Object} grids 格子数 一维
	 * @param {Object} name 层名 
	 * @param {Object} cols 列数
	 * @param {Object} rows 行数
	 * @param {Object} eventSupport 支持事件
	 */
	JadeEngine.Layer = function(grids, name, cols, rows, eventSupport) {
		this.id = GUID.next();
		this.name = name;
		this.cols = cols;
		this.rows = rows;
		this.grids = grids;
		this.type = "background";
		this.eventSupport = eventSupport;
		this.getLocation = function() {
			var x = -1;
			var y = -1;
			if (arguments.length == 1) {
				x = arguments[0].x;
				y = arguments[0].y;
			}

			if (arguments.length == 2) {
				x = arguments[0];
				y = arguments[1];
			}

			var length = grids.length;
			var i = 0;
			for (var _y = 0; _y < rows; _y++) {
				for (var _x = 0; _x < cols; _x++) {
					if (_x == x && _y == y) {
						return {
							"layerId": this.id,
							"x": x,
							"y": y,
							"type": this.type,
							"grid": this.grids[i]
						};
					}
					i++;
				}
			}
			return null;
		}

		this.onEvent = function() {

		}
		if (jadeSystem.isUndefine(this.eventSupport)) {
			this.eventSupport = true;
			JadeEngine.event.pool[this.id] = this;
		}
	}

	/**
	 * 格子
	 * @param {Object} width 宽度
	 * @param {Object} height 高度
	 * @param {Object} eventSupport 支持事件
	 */
	JadeEngine.Grid = function(width, height, eventSupport) {
		this.id = GUID.next();
		this.width = width;
		this.height = height;
		this.eventSupport = eventSupport;
		this.data = null;
		this.onEvent = function() {

		}
		if (jadeSystem.isUndefine(this.eventSupport)) {
			this.eventSupport = true;
			JadeEngine.event.pool[this.id] = this;
		}
	}

	/**
	 * 可视区域
	 * @param {Object} scence 场景
	 * @param {Object} width 宽度
	 * @param {Object} height 高度
	 * @param {Object} eventSupport 支持事件
	 */
	JadeEngine.VisibleWindow = function(scence, width, height, eventSupport) {
		this.id = GUID.next();
		this.scence = scence;
		this.currentLocation = {};
		this.currentLocation.x = scence.startLocation.x;
		this.currentLocation.y = scence.startLocation.y;
		this.width = width;
		this.height = height;

		this.onRender = function(data) {
			JadeEngine.renderEngineMap[JadeEngine.renderEngineType](data);
		}

		this.renderWindow = function() {
			var layerGroup = this.scence.layerGroup;
			for (var x = this.currentLocation.x; x < this.width; x++) {
				for (var y = this.currentLocation.y; y < this.height; y++) {
					this.onRender(layerGroup.getLocations(x, y));
				}
			}
		}

		this.renderInit = function(scence, width, height) {
			this.scence = scence;
			this.currentLocation.x = scence.startLocation.x;
			this.currentLocation.y = scence.startLocation.y;
			if (!jadeSystem.isUndefine(width)) {
				this.width = width;
			}
			if (!jadeSystem.isUndefine(height)) {
				this.height = height;
			}
		}
		this.onEvent = function() {

		}
		if (jadeSystem.isUndefine(this.eventSupport)) {
			this.eventSupport = true;
			JadeEngine.event.pool[this.id] = this;
		}
	}

}());
